<?php
/**
 * Created by PhpStorm.
 * User: Drunk
 * Date: 2017-1-20
 * Time: 21:31
 */

namespace drunk;

class str{
    /**
     * 命名规则转换,支持驼峰法与下划线法
     * @param string $source 命名字串
     * @param int $type {0:转换为下划线发, 1:转换为小驼峰法, 2:转换为大驼峰法}
     * @return string
     **/
    public static function name($source, $type = 0){
        if($type){
            $name = preg_replace_callback('/^\w|_\w/i', create_function('$matches', 'return ucfirst($matches[0]);'), $source);
            if($type == 1) $name = lcfirst($name);
        }else{
            $name = strtolower(preg_replace('/(?!^)[A-Z]/', '_$0', $source));
        }
        return $name;
    }

    /**
     * 转小驼峰法命名
     * @param string $source
     * @return string
     */
    public static function name_camel($source){
        return self::name($source, 1);
    }

    /**
     * 转大驼峰法命名
     * @param string $source
     * @return string
     */
    public static function name_camel_upper($source){
        return self::name($source, 2);
    }

    /**
     * 转下划线法命名
     * @param string $source
     * @return string
     */
    public static function name_underline($source){
        return self::name($source);
    }

    /**
     * 将gb系列编码转为utf8编码
     * @param string $str
     * @return string
     */
    public static function gb_to_utf8($str){
        return mb_convert_encoding($str, 'UTF-8', 'ASCII,UTF-8,GB2312,GBK') ;
    }

    /**
     * @param $test
     * @return int
     */
    public static function is_regexp($test){
        return preg_match('/^\/.+\/\w*$/', $test);
    }
}